﻿using System;
using System.Activities;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Wicresoft.AppFlow.Flows;
using AppLab.Data.Models;

namespace Wicresoft.AppFlow.Activities
{
    public sealed class Publish : StateActivity<FlowState>
    {

        protected override string BookmarkName
        {
            get
            {
                using (var dc = PMSEntities.Create())
                {
                    var flow = dc.tysx_AppFlow.First(p => p.FlowID == cont.WorkflowInstanceId);
                    if (flow.StateID == (int)FlowState.TestSucceeded)
                    {
                        return FlowState.TestSucceeded.ToString();
                    }
                    else
                    {
                        return FlowState.PendingPublish.ToString();
                    }
                }
            }
        }

        protected override FlowState ResumeBookmark(NativeActivityContext context, Bookmark bookmark, object value)
        {
            var e = value as tysx_AppFlow;
            Debug.Assert(e != null, "Entity object [tysx_AppFlow] cannot be null");

            using (var dc = PMSEntities.Create())
            {
                var flow = dc.tysx_AppFlow.First(p => p.FlowID == context.WorkflowInstanceId);
                flow.StateID = e.StateID;
                dc.SaveChanges();
            }
            return (FlowState)e.StateID;
            //throw new NotImplementedException();
        }
    }
}
